﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Manage_MyAnswer : System.Web.UI.Page
{
    protected Question question;

    protected void Page_Load(object sender, EventArgs e)
    {
        string qid = Request.Params["qid"];
            CmsDataContext db = new CmsDataContext();

            question = (from aQ in db.Question
                        where aQ.ID == qid
                        select aQ).First();
        
        if (!IsPostBack)
        {
            #region 加载问题的答案
            DateTime now = DateTime.Now;
            var q = from aA in db.Answer
                    where aA.QuestionID == qid
                    orderby aA.Time descending
                    select new
                    {
                        ID = aA.ID,
                        Name = aA.Teacher.Name,
                        Time = ((now.Year == aA.Time.Year) &&
                        (now.Month == aA.Time.Month) &&
                        (now.Day == aA.Time.Day)) ? string.Format("今天 {0:HH:mm}", aA.Time) : string.Format("{0:yyyy-MM-dd HH:mm", aA.Time),
                        Memo = aA.Memo
                    };
            RecordView.DataSource = q.ToList();
            RecordView.DataBind();
            #endregion
        }
    }
    protected void Save_Click(object sender, EventArgs e)
    {
        CmsDataContext db = new CmsDataContext();

        try
        {
            string qid = Request.Params["qid"];
            
            Answer answer = new Answer();
            answer.ID = Toolkit.NextUUID;
            answer.Memo = Reply.Text.Trim();
            answer.QuestionID = qid;
            answer.TeacherID = Toolkit.CurrentTeacherID;
            answer.Time = DateTime.Now;

            db.Answer.InsertOnSubmit(answer);
            db.SubmitChanges();


            MessageBox.Message = "您的回答已经提交。";
            MessageBox.Location = "./MyAnswer.aspx?qid=" + qid;
        }
        catch (Exception ee)
        {
            MessageBox.Message = "提交回答时发生异常：" + ee.Message;
        }
    }
}